<?php

class EmpresaPeer extends BaseEmpresaPeer
{
    static public function getLuceneIndex()
	{
	  ProjectConfiguration::registerZend();

	  if (file_exists($index = self::getLuceneIndexFile()))
	  {
	    return Zend_Search_Lucene::open($index);
	  }
	  else
	  {
	    return Zend_Search_Lucene::create($index);
	  }
	}

    static public function getLuceneIndexFile()
	{
	  return sfConfig::get('sf_data_dir').'/empresa.'.sfConfig::get('sf_environment').'.index';
	}

	public static function doDeleteAll($con = null)
	{
	  if (file_exists($index = self::getLuceneIndexFile()))
	  {
	    sfToolkit::clearDirectory($index);
	    rmdir($index);
	  }

	  return parent::doDeleteAll($con);
	}
    /*
     * Metodo que se encarga de realizar la busqueda
     */
    static public function getForLuceneQuery($query)
    {
      $hits = self::getLuceneIndex()->find($query);
      
      $pks = array();
      foreach ($hits as $hit)
      {
        $pks[] = $hit->pk;
      }

      $criteria = new Criteria();
      $criteria->add(self::ID, $pks, Criteria::IN);
      $criteria->setLimit(20);

      //var_dump(self::doSelect(self::addEmpresaCriteria($criteria)));
      //return self::doSelect(self::addActiveJobsCriteria($criteria));
      return self::doSelect(self::addEmpresaCriteria($criteria));
    }

    static public function addEmpresaCriteria(Criteria $criteria = null)
  {
    if (is_null($criteria))
    {
      $criteria = new Criteria();
    }
    //$criteria->add(self::EXPIRES_AT, time(), Criteria::GREATER_THAN);
    //$criteria->addDescendingOrderByColumn(self::CREATED_AT);
    //$criteria->add(self::IS_ACTIVATED, true);

    return $criteria;
  }
}

